package com.swt.testdemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swt.testdemo.dao.SysRoleDao;
import com.swt.testdemo.entity.SysRoleEntity;
import com.swt.testdemo.service.SysRoleService;
import com.swt.testdemo.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author BleethNie
 * @version 1.0
 * @date 2020/11/11 14:40
 **/
@Service("SysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public SysRoleEntity queryRoleByRoleType(String roleType) {
        String key = "role:type:"+roleType;
        SysRoleEntity sysRoleEntity = redisUtil.getObject(key, SysRoleEntity.class);
        if (sysRoleEntity == null){
            sysRoleEntity = sysRoleDao.queryRoleByRoleType(roleType);
            redisUtil.setObject(key,sysRoleEntity,RedisUtil.ONE_HOUR_EXPIRE_TIME);
        }
        return sysRoleEntity;
    }

    @Override
    public SysRoleEntity queryRoleById(Integer roleId) {
        String key = "role:id:"+roleId;
        SysRoleEntity sysRoleEntity = redisUtil.getObject(key, SysRoleEntity.class);
        if (sysRoleEntity == null){
            sysRoleEntity=sysRoleDao.queryRoleById(roleId);
            redisUtil.setObject(key,sysRoleEntity,RedisUtil.ONE_HOUR_EXPIRE_TIME);
        }
        return sysRoleEntity;
    }
}
